package com.huanying.goodjob.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.goodjob.skill.Skill;
import com.huanying.goodjob.skill.SkillService;

@Service
public class JobServiceImpl implements JobService {

	@Autowired
	private BaseDao dao;
	@Autowired
	private SkillService skillService;

	@Override
	public Job getbyId_job(int id) throws Exception {
		return (Job) dao.load(Job.class, id);
	}

	@Override
	public void update_job(Job job) throws Exception {
		dao.update(job);
	}

	@Override
	public void add_job(Job job) throws Exception {
		dao.save(job);
	}

	@Override
	public void dalete_job(Job job) throws Exception {
		dao.delete(job);
	}
	
	@Override
	public void delete(int jobitem_id) throws Exception {
		List<Object> param = new ArrayList<Object>();
		String hql = "update Jobitem_Skill set status=0 where jobitem_id=?";
		param.add(jobitem_id);
    	dao.exculeByParams(hql, param);
	}
	
	@Override
	public int add_jobitem(Jobitem jobitem) throws Exception {
		dao.save(jobitem);
		return jobitem.getId();

	}

	@Override
	public void add_skills(int jobitem_id, String[] skillid_array) throws Exception {
		for (int i = 0; i < skillid_array.length; i++) {
			Jobitem_Skill jobitem_skill = new Jobitem_Skill();
			jobitem_skill.setSkill(skillService.getbyId_skill(Integer.valueOf(skillid_array[i])));
			jobitem_skill.setJobitem_id(jobitem_id);
			jobitem_skill.setStatus(1);
			dao.save(jobitem_skill);
		}
	}

	@Override
	public void update_jobitem(Jobitem jobitem) throws Exception {
		dao.update(jobitem);
	}

	@Override
	public void delete_jobitem(Jobitem jobitem) throws Exception {
		dao.delete(jobitem);
	}

	@Override
	public Jobitem getbyId_jobitem(int id) throws Exception {
		return (Jobitem) dao.load(Jobitem.class, id);
	}
	
	public Jobitem_Skill getbyId_jobitem_skill(int id) throws Exception {
		return (Jobitem_Skill) dao.load(Jobitem_Skill.class, id);
	}

	@Override
	public PageBean searchJobs(Map<String, String> map, int pageSize, int page) throws Exception {
		StringBuilder hql = new StringBuilder("from Job as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (!("").equals(map.get("create_time")) && map.get("create_time") != null) {
			hql.append(" and TIMESTAMPDIFF(DAY,DATE_FORMAT(j.create_time,'%Y-%m-%d'),DATE_FORMAT(NOW(),'%Y-%m-%d'))<=? ");
			param.add(Integer.parseInt((String) map.get("create_time")));

		//	hql.append(" and DATE_FORMAT(j.create_time,'%Y-%m-%d') = ?");
		//	param.add(map.get("create_time"));
		}
		
		
		
		if (map.get("community_name") != null && !("").equals(map.get("community_name"))) {
			hql.append(" and j.community like ?");
			param.add("%" + map.get("community_name") + "%");
		}
		if (map.get("user_name") != null && !("").equals(map.get("user_name"))) {
			hql.append(" and j.user.name like ?");
			param.add("%" + map.get("user_name") + "%");
		}
		if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
			Integer user_id = Integer.valueOf(map.get("user_id"));
			hql.append(" and j.user.id = ?");
			param.add(user_id);
		}
		if (map.get("decoration_type") != null && !("").equals(map.get("decoration_type"))) {
			Integer decoration_type = Integer.valueOf(map.get("decoration_type"));
			hql.append(" and j.decoration_type = ?");
			param.add(decoration_type);
		}
		if (map.get("county_code") != null && !("").equals(map.get("county_code"))) {
			hql.append(" and j.area.code = ?");
			param.add(map.get("county_code"));
		}
		if (map.get("release_status") != null && !("").equals(map.get("release_status"))) {
			Integer release_status = Integer.valueOf(map.get("release_status"));
			hql.append(" and j.release_status = ?");
			param.add(release_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}
		

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public Jobitem_User getbyId_jobitem_user(int id) throws Exception {
		return (Jobitem_User) dao.load(Jobitem_User.class, id);
	}

	@Override
	public PageBean searchJobitems(Map<String, String> map, int pageSize, int page) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map != null) {
			if (!("").equals(map.get("end_request_date")) && map.get("end_request_date") != null) {
				hql.append(" and DATE_FORMAT(j.end_request_date,'%Y-%m-%d %T') = ?");
				param.add(map.get("end_request_date"));
			}
			if (map.get("job_id") != null && !("").equals(map.get("job_id"))) {
				Integer job_id = Integer.valueOf(map.get("job_id"));
				hql.append(" and j.job.id = ?");
				param.add(job_id);
			}
			if (map.get("community_name") != null && !("").equals(map.get("community_name"))) {
				hql.append(" and j.job.community like ?");
				param.add("%" + map.get("community_name") + "%");
			}
			if (map.get("county_code") != null && !("").equals(map.get("county_code"))) {
				hql.append(" and j.job.area.code = ?");
				param.add(map.get("county_code"));
			}
			if (map.get("jobtype_name") != null && !("").equals(map.get("jobtype_name"))) {
				hql.append(" and j.jobtype.name like ?");
				param.add("%" + map.get("jobtype_name") + "%");
			}
			if (map.get("jobtype_id") != null && !("").equals(map.get("jobtype_id"))) {
				Integer jobtype_id = Integer.valueOf(map.get("jobtype_id"));
				hql.append(" and j.jobtype.id = ?");
				param.add(jobtype_id);
			}
			if (map.get("decoration_type") != null && !("").equals(map.get("decoration_type"))) {
				Integer decoration_type = Integer.valueOf(map.get("decoration_type"));
				hql.append(" and j.job.decoration_type = ?");
				param.add(decoration_type);
			}
			if (map.get("salary_1") != null && !("").equals(map.get("salary_1")) && map.get("salary_2") != null
					&& !("").equals(map.get("salary_2"))) {
				double salary_1 = Double.valueOf(map.get("salary_1"));
				double salary_2 = Double.valueOf(map.get("salary_2"));
				hql.append(" and j.salary between ? and ?");
				param.add(salary_1);
				param.add(salary_2);
			}
			if (map.get("urgent_status") != null && !("").equals(map.get("urgent_status"))) {
				Integer urgent_status = Integer.valueOf(map.get("urgent_status"));
				hql.append(" and j.urgent_status = ?");
				param.add(urgent_status);
			}
			if (map.get("urgent_fee_1") != null && !("").equals(map.get("urgent_fee_1"))
					&& map.get("urgent_fee_2") != null && !("").equals(map.get("urgent_fee_2"))) {
				double urgent_fee_1 = Double.valueOf(map.get("urgent_fee_1"));
				double urgent_fee_2 = Double.valueOf(map.get("urgent_fee_2"));
				hql.append(" and j.urgent_fee between ? and ?");
				param.add(urgent_fee_1);
				param.add(urgent_fee_2);
			}
			if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
				int jobitem_status = Integer.valueOf(map.get("jobitem_status"));
				hql.append(" and j.jobitem_status = ?");
				param.add(jobitem_status);
			}
			if (map.get("release_status") != null && !("").equals(map.get("release_status"))) {
				Integer release_status = Integer.valueOf(map.get("release_status"));
				hql.append(" and j.job.release_status = ?");
				param.add(release_status);
			}
			if (map.get("status") != null && !("").equals(map.get("status"))) {
				int status = Integer.valueOf(map.get("status"));
				hql.append(" and j.status = ?");
				param.add(status);
			}
			if(map.get("flag") != null &&!("").equals(map.get("flag"))) {
				hql.append(" order by j.end_request_date");
			}
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public void add_jobitem_user(Jobitem_User jobitem_user) throws Exception {
		dao.save(jobitem_user);
	}

	@Override
	public void delete_jobitem_user(Jobitem_User jobitem_user) throws Exception {
		// dao.delete(jobitem_user);
		jobitem_user.setStatus(0);
		dao.update(jobitem_user);
	}

	@Override
	public void update_jobitem_user(Jobitem_User jobitem_user) throws Exception {
		dao.update(jobitem_user);
	}

	@Override
	public PageBean searchJobitem_Users(Map<String, String> map, int pageSize, int page) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem_User as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (!("").equals(map.get("create_time")) && map.get("create_time") != null) {
			hql.append(" and DATE_FORMAT(j.create_time,'%Y-%m-%d %') like ?");
			param.add("%" + map.get("create_time") + "%");
		}
		if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
			Integer user_id = Integer.valueOf(map.get("user_id"));
			hql.append(" and j.user.id = ?");
			param.add(user_id);
		}
		if (map.get("jobitem_id") != null && !("").equals(map.get("jobitem_id"))) {
			Integer jobitem_id = Integer.valueOf(map.get("jobitem_id"));
			hql.append(" and j.jobitem.id = ?");
			param.add(jobitem_id);
		}
		if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
			Integer jobitem_status = Integer.valueOf(map.get("jobitem_status"));
			hql.append(" and j.jobitem_status = ?");
			param.add(jobitem_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public List<Jobitem_User> getbyjobitem_id_jobitem_user(int id) throws Exception {
		String hql = "from Jobitem_User as j where jobitem_id =?";
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		return dao.exculeHqlByParams(hql, params);
	}

	@Override
	public Set<Skill> getbyjobitem_id_skill(int jobitem_id) throws Exception {
		String hql = "from Jobitem_Skill as j where status=1 and jobitem_id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(jobitem_id);
		Set<Skill> skills = new HashSet<Skill>();
		for (Jobitem_Skill jobitem_skill : (List<Jobitem_Skill>) dao.exculeHqlByParams(hql, params)) {
			skills.add(jobitem_skill.getSkill());
		}
		return skills;
	}

	@Override
	public List<Job> getbyuserId_job(int id) throws Exception {
		String hql = "from Job as j where user_id= ?";
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		List<Skill> list = new ArrayList<Skill>();
		return (List<Job>) dao.exculeHqlByParams(hql, params);
	}

	@Override
	public void add_rework(Rework rework) throws Exception {
		dao.save(rework);
	}

	@Override
	public void add_jobitemstatus_change(Jobitemstatus_Change jobitemstatus_change) throws Exception {
		dao.save(jobitemstatus_change);
	}

	@Override
	public void update_jobitemstatus_change(Jobitemstatus_Change jobitemstatus_change) throws Exception {
		dao.update(jobitemstatus_change);
	}

	@Override
	public void dalete_jobitemstatus_change(Jobitemstatus_Change jobitemstatus_change) throws Exception {
		dao.delete(jobitemstatus_change);
	}

	@Override
	public Jobitemstatus_Change getbyId_jobitemstatus_change(int id) throws Exception {
		return (Jobitemstatus_Change) dao.load(Jobitemstatus_Change.class, id);
	}

	@Override
	public void update_rework(Rework rework) throws Exception {
		dao.update(rework);
	}

	@Override
	public void dalete_rework(Rework rework) throws Exception {
		dao.delete(rework);
	}

	@Override
	public Rework getbyId_rework(int id) throws Exception {
		return (Rework) dao.load(Rework.class, id);
	}

	@Override
	public PageBean searchJobitemstatus_change(Map<String, String> map, int pageSize, int page) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitemstatus_Change as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("jobitem_id") != null && !("").equals(map.get("jobitem_id"))) {
			Integer jobitem_id = Integer.valueOf(map.get("jobitem_id"));
			hql.append(" and j.jobitem.id = ?");
			param.add(jobitem_id);
		}
		if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
			Integer jobitem_status = Integer.valueOf(map.get("jobitem_status"));
			hql.append(" and j.jobitem_status = ?");
			param.add(jobitem_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}
		if (!("").equals(map.get("time")) && map.get("time") != null) {
			hql.append(" and DATE_FORMAT(j.time,'%Y-%m-%d %') like ?");
			param.add("%" + map.get("time") + "%");
		}

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public List<Object> searchJobs(Map<String, String> map) throws Exception {
		StringBuilder hql = new StringBuilder("from Job as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("user_name") != null && !("").equals(map.get("user_name"))) {
			hql.append(" and j.user.name like ?");
			param.add("%" + map.get("user_name") + "%");
		}
		if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
			Integer user_id = Integer.valueOf(map.get("user_id"));
			hql.append(" and j.user.id = ?");
			param.add(user_id);
		}
		if (map.get("county_code") != null && !("").equals(map.get("county_code"))) {
			hql.append(" and j.area.code = ?");
			param.add(map.get("county_code"));
		}
		if (map.get("decoration_type") != null && !("").equals(map.get("decoration_type"))) {
			Integer decoration_type = Integer.valueOf(map.get("decoration_type"));
			hql.append(" and j.decoration_type = ?");
			param.add(decoration_type);
		}
		if (map.get("release_status") != null && !("").equals(map.get("release_status"))) {
			Integer release_status = Integer.valueOf(map.get("release_status"));
			hql.append(" and j.release_status = ?");
			param.add(release_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}
		if (map.get("flag") != null && !("").equals(map.get("flag"))) {
			hql.append(" order by j.job_status");
		}

		List<Object> list = dao.exculeHqlByParams(hql.toString(), param);

		return list;
	}

	@Override
	public List<Object> searchJobitems(Map<String, String> map) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map != null) {
			if (!("").equals(map.get("end_request_date_1")) && map.get("end_request_date_1") != null
					&& !("").equals(map.get("end_request_date_2")) && map.get("end_request_date_2") != null) {
				hql.append(" and DATE_FORMAT(j.end_request_date,'%Y-%m-%d %T') between ? and ?");
				param.add(map.get("end_request_date_1"));
				param.add(map.get("end_request_date_2"));
			}
			if (map.get("job_id") != null && !("").equals(map.get("job_id"))) {
				Integer job_id = Integer.valueOf(map.get("job_id"));
				hql.append(" and j.job.id = ?");
				param.add(job_id);
			}
			if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
				Integer user_id = Integer.valueOf(map.get("user_id"));
				hql.append(" and j.job.user.id = ?");
				param.add(user_id);
			}
			if (map.get("county_code") != null && !("").equals(map.get("county_code"))) {
				hql.append(" and j.job.area.code = ?");
				param.add(map.get("county_code"));
			}
			if (map.get("decoration_type") != null && !("").equals(map.get("decoration_type"))) {
				Integer decoration_type = Integer.valueOf(map.get("decoration_type"));
				hql.append(" and j.job.decoration_type = ?");
				param.add(decoration_type);
			}
			if (map.get("salary_1") != null && !("").equals(map.get("salary_1")) && map.get("salary_2") != null
					&& !("").equals(map.get("salary_2"))) {
				double salary_1 = Double.valueOf(map.get("salary_1"));
				double salary_2 = Double.valueOf(map.get("salary_2"));
				hql.append(" and j.salary between ? and ?");
				param.add(salary_1);
				param.add(salary_2);
			}
			if (map.get("urgent_status") != null && !("").equals(map.get("urgent_status"))) {
				Integer urgent_status = Integer.valueOf(map.get("urgent_status"));
				hql.append(" and j.urgent_status = ?");
				param.add(urgent_status);
			}
			if (map.get("urgent_fee_1") != null && !("").equals(map.get("urgent_fee_1"))
					&& map.get("urgent_fee_2") != null && !("").equals(map.get("urgent_fee_2"))) {
				double urgent_fee_1 = Double.valueOf(map.get("urgent_fee_1"));
				double urgent_fee_2 = Double.valueOf(map.get("urgent_fee_2"));
				hql.append(" and j.urgent_fee between ? and ?");
				param.add(urgent_fee_1);
				param.add(urgent_fee_2);
			}
			if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
				int jobitem_status = Integer.valueOf(map.get("jobitem_status"));
				hql.append(" and j.jobitem_status = ?");
				param.add(jobitem_status);
			}
			if (map.get("status") != null && !("").equals(map.get("status"))) {
				int status = Integer.valueOf(map.get("status"));
				hql.append(" and j.status = ?");
				param.add(status);
			}
		}

		List<Object> list = dao.exculeHqlByParams(hql.toString(), param);

		return list;
	}

	@Override
	public List<Object> searchJobitem_Users(Map<String, String> map) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem_User as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
			Integer user_id = Integer.valueOf(map.get("user_id"));
			hql.append(" and j.user.id = ?");
			param.add(user_id);
		}
		if (map.get("jobitem_id") != null && !("").equals(map.get("jobitem_id"))) {
			Integer jobitem_id = Integer.valueOf(map.get("jobitem_id"));
			hql.append(" and j.jobitem.id = ?");
			param.add(jobitem_id);
		}
		if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
			Integer jobitem_status = Integer.valueOf(map.get("jobitem_status"));
			hql.append(" and j.jobitem_status = ?");
			param.add(jobitem_status);
		}
		if (map.get("jobitem_status_jobitem") != null && !("").equals(map.get("jobitem_status_jobitem"))) {
			Integer jobitem_status = Integer.valueOf(map.get("jobitem_status_jobitem"));
			hql.append(" and j.jobitem.jobitem_status = ?");
			param.add(jobitem_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}

		hql.append(" order by j.id desc");

		List<Object> list = dao.exculeHqlByParams(hql.toString(), param);

		return list;

	}

	@Override
	public List<Object> searchJobitemstatus_change(Map<String, String> map) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitemstatus_Change as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("jobitem_id") != null && !("").equals(map.get("jobitem_id"))) {
			Integer jobitem_id = Integer.valueOf(map.get("jobitem_id"));
			hql.append(" and j.jobitem.id = ?");
			param.add(jobitem_id);
		}
		if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
			Integer jobitem_status = Integer.valueOf(map.get("jobitem_status"));
			hql.append(" and j.jobitem_status = ?");
			param.add(jobitem_status);
		}
		if (map.get("status") != null && !("").equals(map.get("status"))) {
			Integer status = Integer.valueOf(map.get("status"));
			hql.append(" and j.status = ?");
			param.add(status);
		}
		if (!("").equals(map.get("time")) && map.get("time") != null) {
			hql.append(" and DATE_FORMAT(j.time,'%Y-%m-%d %') like ?");
			param.add("%" + map.get("time") + "%");
		}
		hql.append("or j.jobitem.jobitem_status = 0 or j.jobitem.jobitem_status = 2");
		List<Object> list = dao.exculeHqlByParams(hql.toString(), param);
		return list;
	}

	@Override
	public PageBean searchJobitems_index(Map<String, String> map, int pageSize, int page) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map != null) {
			if (map.get("county_code") != null && !("").equals(map.get("county_code"))) {
				hql.append(" and j.job.area.code = ?");
				param.add(map.get("county_code"));
			}
			if (map.get("jobitem_status") != null && !("").equals(map.get("jobitem_status"))) {
				int jobitem_status = Integer.valueOf(map.get("jobitem_status"));
				hql.append(" and j.jobitem_status = ?");
				param.add(jobitem_status);
			}
			if (map.get("release_status") != null && !("").equals(map.get("release_status"))) {
				int release_status = Integer.valueOf(map.get("release_status"));
				hql.append(" and j.job.release_status = ?");
				param.add(release_status);
			}
			if (map.get("status") != null && !("").equals(map.get("status"))) {
				int status = Integer.valueOf(map.get("status"));
				hql.append(" and j.status = ?");
				param.add(status);
			}
			if (map.get("j_status") != null && !("").equals(map.get("j_status"))) {
				hql.append("and j.jobitem_status in ( 0 , 2)");
			}
			//搜索框
			if (map.get("jobtype_name") != null && !("").equals(map.get("jobtype_name"))
					&& map.get("community_name") != null && !("").equals(map.get("community_name"))) {
				hql.append(" and (j.jobtype.name like ? or j.job.community like ? )");
				param.add("%" + map.get("jobtype_name") + "%");
				param.add("%" + map.get("community_name") + "%");
			}
			//工种筛选
			if (map.get("jobtype_id") != null && !("").equals(map.get("jobtype_id"))) {
				String jobtype_id = map.get("jobtype_id");
				String[] jobtypeid_array = jobtype_id.split("_");
				hql.append(" and j.jobtype.id in (");
				for (int i = 0; i < jobtypeid_array.length; i++) {
					int jobtype_id_i = Integer.valueOf(jobtypeid_array[i]);
					hql.append(" ?,");
					param.add(jobtype_id_i);
				}
				hql = new StringBuilder(hql.substring(0, hql.length() - 1));
				hql.append(")");
			}
			//去除当前用户发布的jobitem
			if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
				//去除当前用户发布的jobitem
				hql.append(" and j.job.user.id <> ?");
				param.add(Integer.valueOf(map.get("user_id")));
				//去除当前用户接单的jobitem
				hql.append(" and j.id not in ( select ju.jobitem.id from Jobitem_User as ju where ju.user.id = ? or ju.status = 0)");
				param.add(Integer.valueOf(map.get("user_id")));
				
			}		
		}

		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public List<Object> searchJobitem_Users_his(Map<String, String> map) throws Exception {
		StringBuilder hql = new StringBuilder("from Jobitem_User as j where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("user_id") != null && !("").equals(map.get("user_id"))) {
			Integer user_id = Integer.valueOf(map.get("user_id"));
			hql.append(" and j.user.id = ?");
			param.add(user_id);
		}
		hql.append(" and j.jobitem.jobitem_status < 0 and j.status = 1");
		hql.append(" order by j.jobitem.jobitem_status desc");
		List<Object> list = dao.exculeHqlByParams(hql.toString(), param);

		return list;

	}
}
